package com.ruoyi.monitor.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.monitor.domain.Pollutant;
import com.ruoyi.monitor.mapper.PollutantMapper;
import com.ruoyi.monitor.service.IPolluantAvgService;
import com.ruoyi.monitor.service.IPollutantService;
import com.ruoyi.monitor.utils.Common.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


/**
 * 污染物Service业务层处理
 * 
 * @author xzx
 * @date 2023-01-04
 */
@Service
public class PollutantServiceImpl extends ServiceImpl<PollutantMapper,Pollutant> implements IPollutantService
{
    @Autowired
    private PollutantMapper pollutantMapper;
    @Autowired
    private IPolluantAvgService avgService;
    SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    public List<Pollutant> trainList(Integer limit) {
        QueryWrapper<Pollutant> wrapper = new QueryWrapper<Pollutant>()
                .orderByDesc("ct")
                .last("LIMIT "+limit);
        return pollutantMapper.selectList(wrapper);
    }

    @Override
    public List<Pollutant> selectLatestDays(Integer days) {
        List<Pollutant> res = new ArrayList<>();
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = days; i >= 0; i--) {
            if (i==0){
                SimpleDateFormat ssm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                QueryWrapper<Pollutant> day = new QueryWrapper<Pollutant>().orderByDesc("ct")
                        .apply(
                                //一天内的数据
                                "ct >= " + "'"+ssm.format(DateUtil.beginOfDay(new Date()))+"'")
                        .apply(
                                "ct <= " + "'"+ssm.format(DateUtil.endOfDay(new Date()))+"'")
                        .groupBy("area", "ct") // 添加 GROUP BY 子句
                        .select("primary_pollutant","avg(aqi) as aqi","area","area_code",
                                "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                                "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");

                Pollutant pollutantDay = pollutantMapper.selectOne(day);
                if (!ObjectUtils.isEmpty(pollutantDay)) res.add(pollutantDay);
            }else {
                QueryWrapper<Pollutant> select = new QueryWrapper<Pollutant>().orderByDesc("ct")
                        .apply(
                                //周期内的数据
                                "ct >= " + "'"+sim.format(DateUtils.getPastDate(i,DateUtil.beginOfDay(new Date())))+"'")
                        .apply(
                                "ct <= " + "'"+sim.format(DateUtils.getPastDate(i-1,DateUtil.endOfDay(new Date())))+"'")
                        .groupBy("area", "ct") // 添加 GROUP BY 子句
                        .select("primary_pollutant","avg(aqi) as aqi","area","area_code",
                                "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                                "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");
                Pollutant pollutantWeek = pollutantMapper.selectOne(select);
                if (!ObjectUtils.isEmpty(pollutantWeek)) res.add(pollutantWeek);
            }
        }
        return res;
    }

    @Override
    public Pollutant selectLatestPollutant() {
        return pollutantMapper.selectLatestPollutant();
    }

    /**
     * 查询24小时，1周，一个月的平均数据
     * @return 集合
     */
    @Override
    public List<Pollutant> selectByMulAverage() {
        List<Pollutant> res = new ArrayList<>();

        Pollutant now = pollutantMapper.selectOne(new LambdaQueryWrapper<Pollutant>().orderByDesc(Pollutant::getCt).last("limit 1"));
        System.out.println(now);
        QueryWrapper<Pollutant> day = new QueryWrapper<Pollutant>().orderByDesc("ct")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        //一天内的数据
                        "ct >= " + "'"+sim.format(DateUtil.beginOfDay(now.getCt()))+"'")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        "ct <= " + "'"+sim.format(DateUtil.endOfDay(now.getCt()))+"'")
                .select("id","primary_pollutant","aqi","area","area_code",
                        "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                        "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");

        Pollutant pollutantDay = pollutantMapper.selectOne(day);
        System.out.println(pollutantDay);

        QueryWrapper<Pollutant> week = new QueryWrapper<Pollutant>().orderByDesc("ct")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        //一周内的数据
                        "ct >= " + "'"+sim.format(DateUtil.beginOfWeek(now.getCt()))+"'")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        "ct <= " + "'"+sim.format(DateUtil.endOfWeek(now.getCt()))+"'")
                .select("id","primary_pollutant","aqi","area","area_code",
                        "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                        "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");
        Pollutant pollutantWeek = pollutantMapper.selectOne(week);
        System.out.println(pollutantWeek);

        QueryWrapper<Pollutant> month = new QueryWrapper<Pollutant>().orderByDesc("ct")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        //一个月内的数据
                        "ct >= " + "'"+sim.format(DateUtil.beginOfMonth(now.getCt()))+"'")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        "ct <= " + "'"+sim.format(DateUtil.endOfMonth(now.getCt()))+"'")
                .select("id","primary_pollutant","aqi","area","area_code",
                        "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                        "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");
        Pollutant pollutantMonth = pollutantMapper.selectOne(month);
        System.out.println(pollutantMonth);



        if (!ObjectUtils.isEmpty(pollutantDay)) res.add(pollutantDay);
        if (!ObjectUtils.isEmpty(pollutantWeek)) res.add(pollutantWeek);
        if (!ObjectUtils.isEmpty(pollutantMonth)) res.add(pollutantMonth);

        return res;
    }

    @Override
    public Pollutant CountAVGDay() {
        Pollutant now = pollutantMapper.selectOne(new LambdaQueryWrapper<Pollutant>().orderByDesc(Pollutant::getCt).last("limit 1"));
        System.out.println(now);
        QueryWrapper<Pollutant> day = new QueryWrapper<Pollutant>().orderByDesc("ct")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        //一天内的数据
                        "ct >= " + "'"+sim.format(DateUtil.beginOfDay(now.getCt()))+"'")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        "ct <= " + "'"+sim.format(DateUtil.endOfDay(now.getCt()))+"'")
                .select("id","primary_pollutant","avg(aqi) as aqi","area","area_code",
                        "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                        "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");

        Pollutant pollutantDay = pollutantMapper.selectOne(day);

        return pollutantDay;
    }

    @Override
    public Set<Pollutant> CountAVGDayAll() {
        Set<Pollutant> res = new TreeSet<>();
        List<Pollutant> pollutants = pollutantMapper.selectList(null);

        for (Pollutant now: pollutants) {
            QueryWrapper<Pollutant> day = new QueryWrapper<Pollutant>().orderByDesc("ct")
                    .apply(!ObjectUtils.isEmpty(now.getCt()),
                            //一天内的数据
                            "ct >= " + "'"+sim.format(DateUtil.beginOfDay(now.getCt()))+"'")
                    .apply(!ObjectUtils.isEmpty(now.getCt()),
                            "ct <= " + "'"+sim.format(DateUtil.endOfDay(now.getCt()))+"'")
                    .select("id","primary_pollutant","avg(aqi) as aqi","area","area_code",
                            "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                            "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");
            Pollutant pollutantDay = pollutantMapper.selectOne(day);
            res.add(pollutantDay);
        }
        System.out.println("数据总量"+res.size());
        return res;
    }

    @Override
    public Pollutant CountAVGWeek() {
        Pollutant now = pollutantMapper.selectOne(new LambdaQueryWrapper<Pollutant>().orderByDesc(Pollutant::getCt).last("limit 1"));

        QueryWrapper<Pollutant> week = new QueryWrapper<Pollutant>().orderByDesc("ct")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        //一周内的数据
                        "ct >= " + "'"+sim.format(DateUtil.beginOfWeek(now.getCt()))+"'")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        "ct <= " + "'"+sim.format(DateUtil.endOfWeek(now.getCt()))+"'")
                .select("id","primary_pollutant","aqi","area","area_code",
                        "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                        "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");
        Pollutant pollutantWeek = pollutantMapper.selectOne(week);
        return pollutantWeek;
    }

    @Override
    public Set<Pollutant> CountAVGWeekAll() {
        return null;
    }

    @Override
    public Pollutant CountAVGMonth() {
        Pollutant now = pollutantMapper.selectOne(new LambdaQueryWrapper<Pollutant>().orderByDesc(Pollutant::getCt).last("limit 1"));

        QueryWrapper<Pollutant> month = new QueryWrapper<Pollutant>().orderByDesc("ct")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        //一个月内的数据
                        "ct >= " + "'"+sim.format(DateUtil.beginOfMonth(now.getCt()))+"'")
                .apply(!ObjectUtils.isEmpty(now.getCt()),
                        "ct <= " + "'"+sim.format(DateUtil.endOfMonth(now.getCt()))+"'")
                .select("id","primary_pollutant","aqi","area","area_code",
                        "quality","num","ct","o38h","avg(pm25) as pm25","avg(pm10) as pm10","avg(co) as co",
                        "avg(so2) as so2","avg(no2) as no2","avg(o3) as o3");
        Pollutant pollutantMonth = pollutantMapper.selectOne(month);
        return pollutantMonth;
    }

    @Override
    public Set<Pollutant> CountAVGMonthAll() {
        return null;
    }


    @Override
    public List<Pollutant> selectPollutantList()
    {
        LambdaQueryWrapper<Pollutant> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Pollutant::getCt).last("limit 24");
            return pollutantMapper.selectList(wrapper);

    }

    @Override
    public List<Pollutant> selectPollutantListCondition(Pollutant pollutant) {
        if (pollutant.getCt()!=null)        {
            pollutant.setCt(DateUtil.parse(DateUtils.formatMD(pollutant.getCt()),"yyyy-MM-dd"));
            System.out.println(pollutant.getCt());
        }

        return pollutantMapper.selectPollutantList(pollutant);
    }

    /**
     * 新增污染物
     *
     * @param pollutant 污染物
     * @return 结果
     */
    @Override
    public int insertPollutant(Pollutant pollutant)
    {
        return pollutantMapper.insert(pollutant);
    }

    @Override
    public Pollutant findPollutant(Pollutant pollutant) {
        LambdaQueryWrapper<Pollutant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pollutant::getCt,sim.format(pollutant.getCt()));
        return pollutantMapper.selectOne(wrapper);
    }


}
